package com.baizhiedu.baizhiappsys.dao.impl;

import com.baizhiedu.baizhiappsys.dao.UserDao;
import com.baizhiedu.baizhiappsys.entity.User;
import com.baizhiedu.baizhiappsys.orm.UserRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author lby
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(User record) {
        return 0;
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public List<User> selectAll() {
//        查询全部的sql
        String sql = "select * from t_user";

//        执行sql查询  第一个参数sql 第二个行映射
        List<User> query = jdbcTemplate.query(sql, new UserRowMapper());
        return query;
    }

    @Override
    public List<User> selectAllByLimit(Integer offset, Integer limit) {
        //        查询全部的sql
        String sql = "select * from t_user limit "+offset+","+limit;

//        执行sql查询  第一个参数sql 第二个行映射(自己做的映射对象)
        List<User> query = jdbcTemplate.query(sql, new UserRowMapper());

        return query;
    }

    @Override
    public Integer selectTotalCount() {
        //        查询全部的sql
        String sql = "select COUNT(*) from t_user ";

        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return 0;
    }

    @Override
    public User searchUserByPhone(String searchContext) {
        // 条件查询
        String sql = "select * from t_user where phonenumber = ?";
        User user = jdbcTemplate.queryForObject(sql, new UserRowMapper(), searchContext);
        return user;
    }
}
